<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section [formGroup]="credentialsMqttFormGroup">
  <fieldset class="fields-group">
    <legend class="group-title" translate>mqtt-client-credentials.authentication</legend>
    <div class="tb-hint" innerHTML="{{ 'mqtt-client-credentials.hint-credentials-type-basic' | translate }}"></div>
    <mat-form-field class="mat-block">
      <mat-label translate>mqtt-client.client-id</mat-label>
      <input matInput formControlName="clientId">
      <tb-copy-button matSuffix
        [class.!hidden]="!credentialsMqttFormGroup.get('clientId')?.value?.length"
        [copyText]="credentialsMqttFormGroup.get('clientId').value">
      </tb-copy-button>
      <button mat-icon-button matSuffix
        [class.hidden]="disabled()"
        [matTooltip]="'action.regenerate' | translate"
        matTooltipPosition="above"
        (click)="regenerate('clientId')">
        <mat-icon>
          autorenew
        </mat-icon>
      </button>
      <mat-icon matSuffix
                matTooltipClass="tb-error-tooltip"
                matTooltip="{{ 'mqtt-client-credentials.client-id-or-user-name-required' | translate }}"
                [class.!hidden]="!(credentialsMqttFormGroup.hasError('atLeastOne') && credentialsMqttFormGroup.get('clientId').touched)"
                class="tb-error">
        warning
      </mat-icon>
    </mat-form-field>
    <div class="flex flex-col">
      <div class="flex flex-row xs:flex-col gt-xs:gap-2">
        <mat-form-field class="flex flex-1">
          <mat-label translate>mqtt-client-credentials.username</mat-label>
          <input matInput formControlName="userName" name="user-name">
          <tb-copy-button matSuffix
            [class.!hidden]="!credentialsMqttFormGroup.get('userName')?.value?.length"
            [copyText]="credentialsMqttFormGroup.get('userName').value">
          </tb-copy-button>
          <button mat-icon-button matSuffix
            [class.hidden]="disabled()"
            [matTooltip]="'action.regenerate' | translate"
            matTooltipPosition="above"
            (click)="regenerate('userName')">
            <mat-icon>
              autorenew
            </mat-icon>
          </button>
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'mqtt-client-credentials.client-id-or-user-name-required' | translate }}"
                    [class.!hidden]="!(credentialsMqttFormGroup.hasError('atLeastOne') && credentialsMqttFormGroup.get('userName').touched)"
                    class="tb-error">
            warning
          </mat-icon>
        </mat-form-field>
        @if (!entity()?.id) {
          <mat-form-field class="flex flex-1">
            <mat-label translate>mqtt-client-credentials.password</mat-label>
            <input matInput formControlName="password"
              autocomplete="new-password" name="new-password"
              type="password">
            <tb-toggle-password matSuffix></tb-toggle-password>
          </mat-form-field>
        }
      </div>
    </div>
  </fieldset>
  <mat-expansion-panel [expanded]="true">
    <mat-expansion-panel-header>
      <mat-panel-title>
        <div translate>mqtt-client-credentials.authorization</div>
      </mat-panel-title>
    </mat-expansion-panel-header>
    <section>
      <div class="tb-hint pt-0" [innerHtml]="'mqtt-client-credentials.hint-authorization-basic' | translate"></div>
      <div class="tb-hint" [innerHtml]="'mqtt-client-credentials.hint-regex-patterns' | translate"></div>
    </section>
    <div class="mt-2 flex flex-1 flex-col" formGroupName="authRules">
      <mat-form-field class="mat-block">
        <mat-label translate>mqtt-client-credentials.authorization-rule-patterns-pub</mat-label>
        <mat-chip-grid #chipListPub formControlName="pubAuthRulePatterns">
          @for (rule of pubRulesSet; track rule) {
            <mat-chip-row
              [value]="rule"
              [editable]="!disabled()"
              (edited)="editTopicRule($event, authRulePatternsType.PUBLISH)"
              [removable]="!disabled()"
              (removed)="removeTopicRule(rule, authRulePatternsType.PUBLISH)">
              {{ rule }}
              @if (!disabled()) {
                <button matChipRemove aria-label="'remove' + rule">
                  <mat-icon>close</mat-icon>
                </button>
              }
            </mat-chip-row>
          }
          <input matInput type="text"
            placeholder="{{ 'mqtt-client-credentials.add-topic-rule' | translate }}"
            matChipInputAddOnBlur
            [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
            [matChipInputFor]="chipListPub"
            (matChipInputTokenEnd)="addTopicRule($event, authRulePatternsType.PUBLISH)">
        </mat-chip-grid>
        <mat-icon [class.!hidden]="pubRulesSet.size" matSuffix style="color: #ff9a00" [matTooltip]="'mqtt-client-credentials.warning-pub' | translate">
          warning
        </mat-icon>
      </mat-form-field>
      <mat-form-field class="mat-block">
        <mat-label translate>mqtt-client-credentials.authorization-rule-patterns-sub</mat-label>
        <mat-chip-grid #chipListSub formControlName="subAuthRulePatterns">
          @for (rule of subRulesSet; track rule) {
            <mat-chip-row
              [value]="rule"
              [editable]="!disabled()"
              (edited)="editTopicRule($event, authRulePatternsType.SUBSCRIBE)"
              [removable]="!disabled()"
              (removed)="removeTopicRule(rule, authRulePatternsType.SUBSCRIBE)">
              {{ rule }}
              @if (!disabled()) {
                <button matChipRemove aria-label="'remove' + rule">
                  <mat-icon>close</mat-icon>
                </button>
              }
            </mat-chip-row>
          }
          <input matInput type="text"
            placeholder="{{ 'mqtt-client-credentials.add-topic-rule' | translate }}"
            matChipInputAddOnBlur
            [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
            [matChipInputFor]="chipListSub"
            (matChipInputTokenEnd)="addTopicRule($event, authRulePatternsType.SUBSCRIBE)">
        </mat-chip-grid>
        <mat-icon [class.!hidden]="subRulesSet.size" matSuffix style="color: #ff9a00" [matTooltip]="'mqtt-client-credentials.warning-sub' | translate">
          warning
        </mat-icon>
      </mat-form-field>
    </div>
  </mat-expansion-panel>
</section>
